package com.zhao.dota.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhao.dota.model.*;
import com.zhao.dota.service.*;
import com.zhao.dota.struct.Result;
import com.zhao.dota.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.YearMonth;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/match")
@Slf4j
public class MatchController {

    @Resource
    private MatchService matchService;
    @Resource
    private BanPickService banPickService;
    @Resource
    private MatchPlayerService matchPlayerService;
    @Resource
    private PerformanceService performanceService;
    @Resource
    private MatchPlayerItemService matchPlayerItemService;
    @Resource
    private MatchPlayerAbilityService matchPlayerAbilityService;


    @Value("${server.tomcat.basedir}")
    private String tempPath;

    @GetMapping("/")
    public Result<Page<DotaMatch>> list(Page<DotaMatch> page) {
        page.addOrder(OrderItem.desc("match_id"));
        Page<DotaMatch> data = matchService.page(page);
        return Result.success(data);
    }

    @GetMapping("/{matchId}")
    public Result<Map<String, Object>> detail(@PathVariable Long matchId) {
        DotaMatch match = matchService.getByMatchId(matchId);
        List<BanPick> banPickList = banPickService.findByMatchId(match.getId());
        List<MatchPlayer> players = matchPlayerService.findByMatchId(match.getId());
        List<MatchPlayerItem> playerItems = matchPlayerItemService.findByMatchId(match.getId());
        List<Performance> performances = performanceService.findByMatchId(match.getId());
        Map<Integer, Performance> performanceMap = performances.stream()
                .collect(Collectors.toMap(Performance::getMatchPlayerId, Function.identity()));
        Map<Integer, List<MatchPlayerItemVo>> itemMap = playerItems.stream().map(MatchPlayerItemVo::new)
                .collect(Collectors.groupingBy(MatchPlayerItemVo::getMatchPlayerId));

        List<MatchPlayerAbility> abilities = matchPlayerAbilityService.findByMatchId(match.getId());
        Map<Integer, List<MatchPlayerAbilityVo>> abilityMap = abilities.stream().map(MatchPlayerAbilityVo::new)
                .collect(Collectors.groupingBy(MatchPlayerAbility::getMatchPlayerId));

        Map<String, Object> result = new HashMap<>();
        result.put("match", match);
        result.put("performances", performanceMap);
        result.put("playerItems", itemMap);
        result.put("abilities", abilityMap);
        result.put("banPickList", banPickList.stream().map(BanPickVo::new).collect(Collectors.toList()));
        result.put("players", players.stream().map(MatchPlayerVo::new).collect(Collectors.toList()));
        return Result.success(result);
    }

}
